package com.example.doctor.mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

public interface IMMapper {
    @Select("select a.oc_id_bigint as OCID,b.order_no_nvarchar as OrderNO,"
    +" b.name_nvarchar as Name,c.header_nvarchar as Header,b.age_int as Age,b.sex_nvarchar as Sex,"
    +" a.a_type_int as AType,0 as CType,'' as IMContent,0 as LastTime,d.open_id_nvarchar as OpenID"
    +" from im_online_consultation a"
    +" inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
    +" inner join client_patients_info c on a.pid_bigint = c.pid_bigint"
    +" inner join client_base_info d on c.client_id_bigint = d.uid_bigint"
    +" where a.dc_id_bigint = #{UID} and a.oc_status_tinyint = 1")
    List<Map<String, Object>> IMList(@Param("UID") long UID);
    
    @Select("select a.create_time_datetime as CreateTime,a.im_content_nvarchar as IMContent,cast(a.c_type_tinyint as SIGNED INTEGER) as CType,a.oc_id_bigint as OCID"
    +" from im_consultation_log a"
    +" inner join (select max(create_time_datetime) as LastID from im_consultation_log"
    +" where oc_id_bigint in (${OCIDs})"
    +" group by oc_id_bigint) b on a.create_time_datetime = b.LastID")
    List<Map<String, Object>> LastIM(@Param("OCIDs") String OCIDs);
    
    @Select("select a.oc_id_bigint as OCID,b.order_no_nvarchar as OrderNO, a.pid_bigint as pidBigInt,"
    +" b.name_nvarchar as Name,c.header_nvarchar as Header,b.age_int as Age,b.sex_nvarchar as Sex,e.open_id_nvarchar as OpenID,"
    +" a.a_type_int as AType,b.first_tinyint as First,b.chief_complaint_nvarchar as Chiefcomplaint"
    +" from im_online_consultation a"
    +" inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
    +" inner join client_patients_info c on a.pid_bigint = c.pid_bigint"
    +" inner join order_main d on b.order_no_nvarchar = d.order_no_nvarchar"
    +" inner join client_base_info e on c.client_id_bigint = e.uid_bigint"
    +" where a.dc_id_bigint = #{UID} and a.oc_status_tinyint = 0 and d.system_status_tinyint = 2"
    +" order by d.order_pay_time_datetime")
    List<Map<String, Object>> NoReceiveList(@Param("UID") long UID);

    @Select("select * from ("
    +" select cast(im_type_tinyint as SIGNED INTEGER) as IMType,create_time_datetime as CreateTime,im_content_nvarchar as IMContent,"
    +" cast(c_type_tinyint as SIGNED INTEGER) as CType,cast(d_type_tinyint as SIGNED INTEGER) as DType,rx_order_no_nvarchar as RxOrderNO "
    +" from im_consultation_log"
    +" where oc_id_bigint = #{OCID} "
    +" order by create_time_datetime desc"
    +" limit #{skip},#{limit}) as t order by CreateTime")
    List<Map<String, Object>> IMLogList(@Param("OCID") long OCID, @Param("skip") int skip, @Param("limit") int limit);

    @SelectProvider(type = SqlBuilder.class, method = "allIMListSQL")
    List<Map<String, Object>> allIMList(@Param("beg") Date beg, @Param("end") Date end, @Param("name") String name, 
    @Param("ocStatus") int ocStatus, @Param("dcid") long dcid, @Param("aType") int aType);


    class SqlBuilder {
        public String allIMListSQL(@Param("beg") Date beg, @Param("end") Date end, @Param("name") String name, 
            @Param("ocStatus") int ocStatus, @Param("dcid") long dcid, @Param("aType") int aType) {
            String sql = "with list as ("
                +" select oc_id_bigint,min(oc_status_tinyint) as oc_status_tinyint,max(create_time_datetime) as create_time_datetime from im_online_consultation where dc_id_bigint = #{dcid} and a_type_int = #{aType} and oc_status_tinyint != 3 group by oc_id_bigint)"
                +" select a.oc_id_bigint as OCID,b.order_no_nvarchar as OrderNO,a.oc_status_tinyint as ocStatus,"
                +" b.name_nvarchar as Name,c.header_nvarchar as Header,b.age_int as Age,b.sex_nvarchar as Sex,"
                +" a.a_type_int as AType,0 as CType,'' as IMContent,0 as LastTime,d.open_id_nvarchar as OpenID,"
                    +" a.create_time_datetime as CreateTime "
                +" from list l"
                +" inner join im_online_consultation a on a.oc_id_bigint = l.oc_id_bigint and a.oc_status_tinyint = l.oc_status_tinyint and l.create_time_datetime = a.create_time_datetime"
                +" inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
                +" inner join client_patients_info c on a.pid_bigint = c.pid_bigint"
                +" inner join client_base_info d on c.client_id_bigint = d.uid_bigint"
                +" where a.dc_id_bigint = #{dcid}";
                if(beg != null && end != null)
                    sql += " and l.create_time_datetime > #{beg} and l.create_time_datetime < #{end}";
                if (name != null && name.length() > 0)
                    sql += " and b.name_nvarchar like CONCAT('%',#{name},'%')";
                if (ocStatus != -1)
                    sql += " and l.oc_status_tinyint = #{ocStatus}";
            return sql;
        }
    }
}
